<template>
  <div id="global-header">
    <a-row :wrap="false">
      <a-col flex="200px">
        <router-link to="/">
          <div class="title-bar">
            <img class="logo" src="@/assets/logo.png" alt="logo" />
            <div class="title">云图无界</div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          @click="onMenuClick"
          :items="items"
        />
      </a-col>
      <a-col flex="200px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            <a-dropdown>
              <a-space class="user_info" justify="center" align="center">
                <a-avatar class="avatar" :src="loginUserStore.loginUser.userAvatar" />
                <span>{{ loginUserStore.loginUser.userName ?? '无名' }}</span>
              </a-space>
              <template #overlay>
                <a-menu>
                  <a-menu-item key="/user/profile">
                    <router-link to="/my-space">
                      <UserOutlined />
                      我的空间
                    </router-link>
                  </a-menu-item>
                  <a-menu-item key="/user/logout" @click="doLogout">
                    <LoginOutlined />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button class="login_button" type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { computed, h, ref } from 'vue'
import { HomeOutlined, LoginOutlined, UserOutlined, PlusOutlined } from '@ant-design/icons-vue'
import type { MenuProps } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useUserLoginStore'
import { onMounted } from 'vue'

const loginUserStore = useLoginUserStore()
const originItems = ref<MenuProps['items']>([
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/exchange-vip',
    label: '兑换VIP',
    title: '兑换VIP',
  },
  {
    key: '/add-picture',
    icon: () => h(PlusOutlined),
    label: '创建图片',
    title: '创建图片',
  },
  {
    key: '/admin/user-manager',
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/admin/picture-manager',
    label: '图片管理',
    title: '图片管理',
  },
  {
    key: '/admin/space-manager',
    label: '空间管理',
    title: '空间管理',
  },
  {
    key: '/about',
    label: '关于',
    title: '关于',
  }
])

const items = computed(() => {
  return originItems.value?.filter((menu) => {
    if (menu.key.startsWith('/admin')) {
      // 表示这只有管理员能够访问
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole != 'admin') {
        // 当未登录或者当前用户不是管理员时，则过滤掉该菜单
        return false
      }
    }
    // 谁都能看见的菜单
    return true
  })
})
// 获取菜单

const router = useRouter()
const current = ref<string[]>([''])

const onMenuClick = ({ key }) => {
  router.push({
    path: key,
  })
}

onMounted(async () => {
    await loginUserStore.featureLoginUserGet()
})

router.afterEach((to) => {
  current.value = [to.path]
})
// 退出登录
const doLogout = () => {
  loginUserStore.featureUserLogout()
}
</script>

<style lang="less" scoped>
#global-header {
  .title-bar {
    display: flex;
    align-items: center;
  }
  .title {
    color: black;
    font-size: 18px;
    margin-left: 16px;
  }
  .logo {
    height: 48px;
  }
  .user-login-status {
    display: flex;
    align-items: center;
  }
  .login_button {
    margin-left: 16px;
  }
}
</style>
